package transformFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CaculatePregnentWeek {

	public static long CONST_WEEK = 3600 * 1000 * 24 * 7;

	/**
	 * @param args
	 * @throws ParseException
	 */
	
	public static double CaculateBetweendays(Date pregnentdate) throws ParseException {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		//Date begin = df.parse(pregnentdate);
		Date date = new Date();
		double betweenSeconds = (date.getTime() - pregnentdate.getTime()) / 1000;// 除以1000是为了转换成秒
		double betweendays = betweenSeconds / (24 * 3600);
		return betweendays;
	}
	
	public static int CaculateWeek(Date pregnentdate) throws ParseException {
		int weeks = (int) Math.floor(CaculateBetweendays(pregnentdate)/7);
		return weeks;
	}
	
	public static int CaculateRestdays(Date pregnentdate) throws ParseException {
		int restdays =(int) (CaculateBetweendays(pregnentdate)%7);
		return restdays;
	}

	/**
	 * 字符串转换到时间格式
	 * 
	 * @param dateStr
	 *            需要转换的字符串
	 * @param formatStr
	 *            需要格式的目标字符串 举例 yyyy-MM-dd
	 * @return Date 返回转换后的时间
	 * @throws ParseException
	 *             转换异常
	 */
	public static Date StringToDate(String dateStr, String formatStr) {
		DateFormat sdf = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = sdf.parse(dateStr);
			System.out.println("date:" + date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

}
